package com.sc.nft.admin.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.DistributedLock;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.constant.DistributedLockConstant;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.EquityProps;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.*;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.AirdropGrantRecordService;
import com.sc.nft.service.DigitalCollectionProductInfoService;
import com.sc.nft.service.EquityPropsService;
import com.sc.nft.service.UserInfoService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/AirdropManagement")
@Api(tags="空投管理")
public class AirdropManagementController {
    private final AirdropGrantRecordService airdropGrantRecordService;
    private final UserInfoService userInfoService;
    private final DigitalCollectionProductInfoService productInfoService;
    private final EquityPropsService equityPropsService;

    @PostMapping(value="/airdropGrantRecordList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "空投发放记录",notes="空投发放记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AirdropGrantRecordVO> airdropGrantRecordList(@Valid @RequestBody AirdropGrantRecordRequest request){
       Page<AirdropGrantRecordVO>page= airdropGrantRecordService.airdropGrantRecordList(request.getPageNo(),request.getPageSize(),request.getNickName(),request.getUserTel(),request.getUserUuid(),
                                                        request.getProductUuid(),request.getFirstTitle(),request.getTypeClassify());
        return PageResult.success(page);
    }

    @PostMapping(value="/airdropGrantRecordListExport", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "空投发放记录导出",notes="空投发放记录导出", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AirdropGrantRecordExportVO> airdropGrantRecordListExport(@Valid @RequestBody AirdropGrantRecordExportRequest request){
        if (StrUtil.isBlank(request.getCollectionName()) && ObjectUtil.isNull(request.getStartTime()) && ObjectUtil.isNull(request.getEndTime())){
            Result.fail("藏品名称筛选和时间筛选不能都为空");
        }
        AirdropGrantRecordExportVO result = new AirdropGrantRecordExportVO();
        result.setExcelName("空投明细记录" + DateUtil.format(DateUtil.date(), "yyyy年MM月dd日") + ".xlsx");
        List<AirdropGrantRecordExportListVO> airdropGrantRecordVOList = airdropGrantRecordService.airdropGrantRecordListExport(request.getCollectionName(), request.getStartTime(), request.getEndTime());
        result.setAirdropGrantRecordExportListVOS(airdropGrantRecordVOList);
        return Result.success(result);
    }
    @PostMapping(value="/airdropGrantUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "可发放空投的用户",notes="可发放空投的用户", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AirdropGrantUserVO> airdropGrantUser(@Valid @RequestBody AirdropGrantUserRequest request){
        Page<UserInfo>page= userInfoService.airdropGrantUser(request.getPageNo(),request.getPageSize(),request.getNickName(),request.getUserUuid(),request.getUserTel());
        return PageResult.success(page,AirdropGrantUserVO.class);
    }
    @PostMapping(value="/airdropGrantProduct", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "空投商品",notes="空投商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AirdropGrantproductVO> airdropGrantProduct(@Valid @RequestBody AirdropGrantProductRequest request){
        Page<AirdropGrantproductVO> airdropGrantproductVOPage = productInfoService.airdropGrantProduct(request.toMp(), request.getFirstTitle(), request.getProductUuid(), request.getTypeClassify(),request.getCommodityType());
        return PageResult.success(airdropGrantproductVOPage);
    }
    @PostMapping(value="/airdropGrant", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "发放空投",notes="发放空投", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    @DistributedLock(value="airDrop",pattern = DistributedLockConstant.TRY_LOCK)
    public Result airdropGrant(@Valid @RequestBody AirdropRequest request) throws GlobalException {
        airdropGrantRecordService.airdropGrant(request.getUserIds(),request.getProductId(),request.getGrantNum());
        return Result.success();
    }
    @PostMapping(value="/upLoadAirdropUserExcel", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量空投用户excel", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "uploadFile", value = "文件流对象", required = true,dataType = "__File")})
    @ResponseBody
    @SysMemberLogin
    @DistributedLock(value="airDrop")
    public Result<AirdropUserListVO> upLoadAirdropUserExcel(MultipartFile uploadFile) throws GlobalException, IOException {
        int hour = DateTime.now().getField(DateField.HOUR_OF_DAY);
        AirdropUserListVO airdropUserListVO= airdropGrantRecordService.upLoadAirdropUserExcel(uploadFile);
        return Result.success(airdropUserListVO);
    }

    @PostMapping(value="/batchAirdropGrant", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量发放空投",notes="批量发放空投", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    @DistributedLock(value="airDrop")
    public Result batchAirdropGrant(@Valid @RequestBody BatchAirdropGrantRequest request) throws GlobalException, IOException {
        int hour = DateTime.now().getField(DateField.HOUR_OF_DAY);
        airdropGrantRecordService.batchAirdropGrant(request.getList(),request.getProductId(),request.getGrantNum());
        return Result.success();
    }

    @PostMapping(value="/airdropGrantEquityProps", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "可空投的权益道具",notes="可空投的权益道具", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AirdropGrantEquityPropsVO> airdropGrantEquityProps(@Valid @RequestBody AirdropGrantEquityPropsRequest request){
        return PageResult.success(equityPropsService.airdropGrantEquityProps(request.toMp(),request.getFirstTitle(),request.getTypeClassify()));
    }

    @PostMapping(value="/distributeEquityProps", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "空投权益道具",notes="空投权益道具", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    @DistributedLock(value="distributeEquityProps",pattern = DistributedLockConstant.TRY_LOCK)
    public Result distributeEquityProps(@Valid @RequestBody DistributeEquityPropsRequest request) throws GlobalException {
        if (request.getGrantNum()>15000) {
            throw new GlobalRunTimeException("最大数量15000个");
        }

        EquityProps equityProps = equityPropsService.getById(request.getProductId());
        if(ObjectUtil.isNull(equityProps)){
            throw new GlobalRunTimeException("权益道具不存在");
        }

        if(!equityProps.getStatus()){
            throw new GlobalRunTimeException("权益道具未上架");
        }

        airdropGrantRecordService.distributeEquityProps(request.getUserId(),equityProps,request.getGrantNum());
        return Result.success();
    }

    @PostMapping(value="/batchAirdropGrantEquityProps", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量权益道具发放空投",notes="批量权益道具发放空投", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    @DistributedLock(value="airDrop")
    public Result batchAirdropGrantEquityProps(@Valid @RequestBody BatchAirdropGrantEquityPropsRequest request) throws GlobalException, IOException {
        int hour = DateTime.now().getField(DateField.HOUR_OF_DAY);
        if(hour>10&&hour<13){
            return Result.fail("请在10点之前或13点之后进行批量空投。高峰期投会导致二级交易出现卡顿！");
        }
        airdropGrantRecordService.batchAirdropGrantEquityProps(request.getList(),request.getProductId());
        return Result.success();
    }

    @PostMapping(value="/airdropGrantEquityPropsRecordList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "空投发放权益道具记录",notes="空投发放权益道具记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AirdropGrantEquityPropsRecordVO> airdropGrantEquityPropsRecordList(@Valid @RequestBody AirdropGrantEquityPropsRecordRequest request){
        return PageResult.success(airdropGrantRecordService.airdropGrantEquityPropsRecordList(request.getPageNo(),request.getPageSize(),request.getNickName(),request.getUserTel(),request.getFirstTitle(),request.getTypeClassify()));
    }

}
